<?php
/*
 * Geração do Layout da base de Dados
 */
header("Content-type: text/html; charset=utf-8");

$space = "";
$out .= sprintf("%s          <div id='tabs'>\n", $space);
$outT = sprintf("%s          <ul>\n", $space);
$ndx = 1;
$ndx = 1;
$first = 0;
$nabas = 0;
$ABAS = "";
$outS = "";
$outS .= sprintf("%s          <form method='post' name='frmdata' id='frmdata' action='' enctype='multipart/form-data'>\n", $space);
$outS .= sprintf("%s          <input type='hidden' name='save' id='save' value='' />\n", $space);
//$outS .= sprintf("%s          <input type='hidden' name='id%s' id='id%s' value='0' />\n", $space,strtolower($TABLE),strtolower($TABLE));
$outS .= sprintf("%s          <fieldset>\n", $space);
$outS .= sprintf("%s          <legend></legend>\n", $space);
$lastAba = "";
$sql = sprintf("SELECT aba,campo,tipo,tamanho,label,linha,coluna,pesquisa FROM Page_mount WHERE Tabela='%s' AND script='%s' AND Linha > '0' ORDER BY aba_order,aba,Linha,coluna", $TABLE, $SCRIPT);
$result = mysql_query($sql, $dbctl);
if($result == 0)
{
     printf("<h1>(PrepIncluir-2)Falha: %s</h1>\n", mysql_error($dbctl));
     printf("<h3>%s</h3>\n", $sql);
     exit;
}
else
{
     if(mysql_num_rows($result) > 0)
     {
          $aba = "";
          $tab = 1;
          $linha = 0;
          while($myrow = mysql_fetch_row($result))
          {
               if($myrow[0] != $lastAba)
               {
                    if($lastAba != "")
                    {
                         $outS .= "                    </div>\n";
                    }
                    $ABA[$nabas] = $myrow[0];
                    $nabas++;
                    $outS .= sprintf("                    <div id=\"tabs-%02d\">\n", $nabas);
                    $lastAba = $myrow[0];
               }
               if($linha == 0)
               {
                    $linha = $myrow[5];
               }
               if($linha != $myrow[5])
               {
                    $outS .= "                         <br />\n";
                    $linha = $myrow[5];
               }
               $tipo = $myrow[2];
               $size = $myrow[3] + 5;
               $size1 = $myrow[3];
               if($size1 > 80)
               {
                    $size1 = 80;
               }
               if(substr(strtolower($myrow[1]), 0, 2) == "id")
               {
                    $size = 80;
               }
               $outS .= sprintf("%s                         <span class='label'>%s:</span>\n", $space, ucfirst($myrow[4]));
               if(substr(strtolower($myrow[1]), 0, 2) == "id")
               {
                    $tb2 = "";
                    $sel = ucfirst(substr($myrow[1], 2, strlen($myrow[1]) - 2));
                    if(strstr($sel, "_id") != "")
                    {
                         $tb2 = strstr($sel, "_id");
                         $sel = substr($sel, 0, strlen($sel) - strlen($tb2));
                    }
                    if($tb2 == "")
                    {
                         if($myrow[7] == "1")
                         {
                              $outS .= sprintf("%s                         <span class='entry_pesq'><input type='text' id='PESQ_ID%s' name='PESQ_ID%s' size='80' tabindex='%d' class='text ui-widget-content ui-corner-all'/></span>\n", $space, strtoupper($sel), strtoupper($sel), $ndx++);
                         }
                         else
                         {
                              $outS .= sprintf("%s                         <span class='entrada'><?php Sel%s(\$db, %d);?></span>\n", $space, $sel, $ndx++);
                         }
                    }
                    else
                    {
                         $outS .= sprintf("%s                         <span class='entrada'><select name='%s' id='%s' tabindex='%d'></select></span>\n", $space, strtoupper($myrow[1]), strtoupper($myrow[1]), $ndx++);
                    }
               }
               else if($tipo == "varchar" || $tipo == "char" || strstr($tipo, "smallint") != "" || strstr($tipo, "tinyint") != "" || strstr($tipo, "int") != "")
               {
                    if(strtoupper($myrow[1]) == "SENHA" || strstr(strtoupper($myrow[1]), "PWD") != "")
                    {
                         $outS .= sprintf("%s                         <span class='entrada'><input type='password' name='%s' id='%s' tabindex='%d' maxlength='%d' size='%d' value='' /></span>\n", $space, strtoupper($myrow[1]), strtoupper($myrow[1]), $ndx++, $size, $size1);
                    }
                    else
                    {
                         $outS .= sprintf("%s                         <span class='entrada'><input type='text' name='%s' id='%s' tabindex='%d' maxlength='%d' size='%d' value='' /></span>\n", $space, strtoupper($myrow[1]), strtoupper($myrow[1]), $ndx++, $size, $size1);
                    }
               }
               else if($tipo == "mediumblob")
               {
                    if(strtoupper($myrow[1]) == 'FOTO')
                    {
                         $outS .= sprintf("%s                         <span class='entrada'><button type='button' id='B%s'>Alterar %s</button><img src='' id='%s' border='0' title='%s'/></span>\n", $space, strtoupper($myrow[1]), ucfirst(strtolower($myrow[1])), strtoupper($myrow[1]), ucfirst($myrow[1]));
                    }
                    else if(strtoupper($myrow[1]) == 'LOGO')
                    {
                         $outS .= sprintf("%s                         <span class='entrada'><button type='button' id='B%s'>Alterar %s</button><img src='' id='%s' border='0' title='%s'/></span>\n", $space, strtoupper($myrow[1]), ucfirst(strtolower($myrow[1])), strtoupper($myrow[1]), ucfirst($myrow[1]));
                    }
                    else if(strtoupper($myrow[1]) == 'DOC')
                    {
                         $outS .= sprintf("%s                         <span class='entrada'><button type='button' id='B%s'>Enviar %s</button></span>\n", $space, strtoupper($myrow[1]), ucfirst(strtolower($myrow[1])));
                    }
                    else if(strtoupper($myrow[1]) == 'DOCX')
                    {
                         $outS .= sprintf("%s                         <span class='entrada'><input id='%s' type='file' name='files[]' multiple></span>\n", $space, strtoupper($myrow[1]), strtoupper($myrow[1]), strtoupper($myrow[1]));
                    }
               }
               else if($tipo == "time")
               {
                    $outS .= sprintf("%s                         <span class='entrada'><input type='text' name='%s' id='%s' tabindex='%d' maxlength='8' size='8' value='' /></span>\n", $space, strtoupper($myrow[1]), strtoupper($myrow[1]), $ndx++);
               }
               else if($tipo == "date")
               {
                    $js .= sprintf("     \$( '#%s').datepicker({changeMonth: true,changeYear: true});\n", strtoupper($myrow[1]));
                    $js .= sprintf("     \$( '#%s').datepicker('option','yearRange','1900:2020');\n", strtoupper($myrow[1]));
                    $outS .= sprintf("%s                         <span class='entrada'><input type='text' name='%s' id='%s' tabindex='%d' size='12' value='' /></span>\n", $space, strtoupper($myrow[1]), strtoupper($myrow[1]), $ndx++);
               }
               else if($tipo == 'datetime')
               {
                    $js .= sprintf("     \$( '#%s').datepicker({changeMonth: true,changeYear: true});\n", strtoupper($myrow[1]));
                    $js .= sprintf("     \$( '#%s').datepicker('option','yearRange','1900:2020');\n", strtoupper($myrow[1]));
                    $outS .= sprintf("%s                         <span class='entrada'><input type='text' name='%s' id='%s' tabindex='%d' size='20' value='' /></span>\n", $space, strtoupper($myrow[1]), strtoupper($myrow[1]), $ndx++);
               }
               else if(strstr($tipo, "text") != "")
               {
                    $outS .= sprintf("%s                         <span class='entrada'><textarea name='%s' id='%s' tabindex='%d' rows='8' cols='80'></textarea></span>\n", $space, strtoupper($myrow[1]), strtoupper($myrow[1]), $ndx++);
               }
               else if(strstr($tipo, "double") != "" || strstr($tipo, "decimal") != "" || strstr($tipo, "float") != "")
               {
                    $outS .= sprintf("%s                         <span class='entrada'><input type='text' name='%s' id='%s' tabindex='%d' maxlength='16' size='16' value='' /></span>\n", $space, strtoupper($myrow[1]), strtoupper($myrow[1]), $ndx++);
               }
               else if(strstr($tipo, "enum") != "")
               {
                    $ret = setEnum($_REQUEST['Server'], $_REQUEST['User'], $_REQUEST['Pwd'], $_REQUEST['DB'], $TABLE, $myrow[1], $ndx++);
                    $outS .= sprintf("%s                         <span class='entrada'>%s</span>\n", $space, $ret);
               }
               else
               {
                    $outS .= sprintf("%s                         <span class='entrada'>&nbsp;</span>\n", $space);
               }
          }
          $outS .= "                    </div>\n";
     }
}
for($n = 0; $n < $nabas; $n++)
{
     $outT .= sprintf("                    <li id='li-tabs-%02d'><a href=\"#tabs-%02d\">%s</a></li>\n", $n + 1, $n + 1, $ABA[$n]);
     SetDir($db,$TABLE,$ABA[$n]);
}
$ret = Mount_Link_Aba($db,$TABLE,$n);
$outT .= $ret;     
$outT .= sprintf("%s          </ul>\n", $space);
$out .= $outT;
$out .= $outS;
$out .= sprintf("%s                    <input type='hidden' name='end' value='1' />\n", $space);
$out .= sprintf("%s               </fieldset>\n", $space);
$out .= sprintf("%s          </form>\n", $space);
$ret = Mount_Link($db,$TABLE,$nabas);
$out .= $ret;
$out .= sprintf("%s               </div>\n", $space);
if(isset($_REQUEST['Debug']))
{
     $out .= sprintf("%s          <textarea name='DEBUG' id='DEBUG' rows='6' cols='40'>Debug</textarea>\n", $space);
}
?>
